//
// Created by francklinson on 2021/7/25 AT 23:51.
//
#include <iostream>
#include <vector>
#include <unordered_map>
#include <queue>
#include <algorithm>

using namespace std;

// Definition for singly-linked list.
struct ListNode {
    int val;
    ListNode *next;

    ListNode(int x) : val(x), next(nullptr) {}
};

class Solution {
public:
    ListNode *getKthFromEnd(ListNode *head, int k) {
        auto node = head;
        for (int i = 0; i < k-1; ++i) {
            node = node->next;
        }
        auto ans = head;
        while (node->next != nullptr) {
            node = node->next;
            ans = ans->next;
        }
        return ans;
    }
};

int main() {
    auto n1 = ListNode(1), n2 = ListNode(2), n3 = ListNode(3), n4 = ListNode(4), n5 = ListNode(5);
    n1.next = &n2;
    n2.next = &n3;
    n3.next = &n4;
    n4.next = &n5;
    Solution solution;
    auto ans = solution.getKthFromEnd(&n1, 3);
    cout << ans->val << endl;
    return 0;
}

